/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.action.server;

import com.yy.bugzillahelp.data.server.ServerConfigData;
import com.yy.bugzillahelp.db.BugzillaHelpDB;
import com.yy.bugzillahelp.tool.SQLTool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hualun-alan
 */
public class ServerConfigAction {

    public static int updateFlushConfig(Connection con, ServerConfigData configData) {
        String sql1 = "update server_info set auto_flush=?,flush_interval=? where id=?";

        PreparedStatement ps1 = null;
        try {
            con.setAutoCommit(false);

            ps1 = con.prepareStatement(sql1);
            ps1.setString(1, configData.isAutoFlush() ? "yes" : "no");
            ps1.setLong(2, configData.getFlushInterval());
            ps1.setString(3, configData.getServerId());
            ps1.executeUpdate();

            con.commit();
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, "sql1 = {0}", sql1);
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps1);
        }
    }

    public static int updateLastFlushTime(Connection con, String serverId, long time) {
        String sql1 = "update server_info set last_flush_time = ? where id=?";

        PreparedStatement ps1 = null;
        try {
            con.setAutoCommit(false);

            ps1 = con.prepareStatement(sql1);
            ps1.setLong(1, time);
            ps1.setString(2, serverId);
            ps1.executeUpdate();

            con.commit();
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, "sql1 = {0}", sql1);
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps1);
        }
    }

    public static ServerConfigData searchByServerId(Connection con, String serverId) {
        ServerConfigData data = new ServerConfigData();

        String sql = "select * from server_info where id = ?";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, serverId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                data.setServerId(rs.getString("id"));
                data.setAutoFlush(rs.getBoolean("auto_flush"));
                data.setFlushInterval(rs.getLong("flush_interval"));
                data.setLastFlushTime(rs.getLong("last_flush_time"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, "sql = {0}", sql);
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            SQLTool.closeStatement(ps);
        }

        return data;
    }

    private static int clearTable(Connection con) {
        String sql = "delete from server_info";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, "sql = {0}", sql);
            Logger.getLogger(ServerConfigAction.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps);
        }
    }

    public static void main(String[] args) {
        Connection con = BugzillaHelpDB.getConnection();
//        System.out.println("clearTable=" + ServerConfigAction.clearTable(con));
//        System.out.println("insertOne=" + updateFlushConfig(con, new ServerConfigData("1", true, 100)));
//        System.out.println("searchByServerId=" + searchByServerId(con, "1"));
//        System.out.println("updateOne=" + updateFlushConfig(con, new ServerConfigData("1", true, 300)));
        updateLastFlushTime(con, "2012-12-21 11:29:15", System.currentTimeMillis());
        System.out.println("searchByServerId=" + searchByServerId(con, "2012-12-21 11:29:15"));
        SQLTool.closeConnection(con);
    }
}
